package com.boc.langxiang.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.DemoContract;
import com.boc.langxiang.presenter.DemoPresenter;
import com.boc.langxiang.ui.adapter.DemoAdapter;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DemoActivity extends BaseToolBarActivity implements SwipeRefreshLayout.OnRefreshListener
        , BaseQuickAdapter.RequestLoadMoreListener, DemoContract.View {

    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    private ArrayList<String> mDatas;
    private static final int TOTAL_COUNT = 45;
    private DemoAdapter mAdapter;
    private DemoContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        ButterKnife.bind(this);
        setToolBarTitle("Demo");

        mPresenter = new DemoPresenter(this);

        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new LinearLayoutManager(mContext));

        initAdapter();
        addHeadView();
    }

    private void initAdapter() {
        mDatas = new ArrayList<>();
        mDatas.addAll(initTestData());
        mAdapter = new DemoAdapter(mContext, mDatas);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.setEmptyView(emptyView);
//        mAdapter.openLoadAnimation();
        //控制itemview动画是否只在第一次显示
//        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mRecylerview.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                T.showShort(mContext, position + "");
            }
        });
    }

    private void addHeadView() {
        View headView = LayoutInflater.from(mContext).inflate(R.layout.header_demo, null, false);

        mAdapter.addHeaderView(headView);
    }

    private List<String> initTestData() {
        ArrayList<String> datas = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            datas.add("i" + i);
        }
        return datas;
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.setNewData(initTestData());
                mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
                mSwipeLayout.setRefreshing(false);
            }
        }, 2000);
    }

    @Override
    public void onLoadMoreRequested() {
        L.i("onLoadMoreRequested");
        mRecylerview.post(new Runnable() {
            @Override
            public void run() {
                if (mAdapter.getData().size() == TOTAL_COUNT) {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                    View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) mRecylerview.getParent(), false);
                    mAdapter.addFooterView(view);
                } else {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataChangedAfterLoadMore(initTestData(), true);
                        }
                    }, 2000);

                }
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(String code, String msg) {

    }

}
